package org.springblade.common.utils.autoid.config;

/**
 * @author Jackey
 * @date 2021年08月18日 10:07 上午
 */

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Component;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * redis工具类
 */
@Component
public class RedisService {
	@Autowired
	private RedisTemplate redisTemplate;

	/**
	 * 写入缓存
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean set(final String key, Object value) {
		boolean result = false;
		try{
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			result = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 写入缓存设置时效时间
	 * @param key
	 * @param value
	 * @return
	 */
	public boolean setEx(final String key, Object value, Long expireTime) {
		boolean result = false;
		try{
			ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
			operations.set(key, value);
			redisTemplate.expire(key, expireTime, TimeUnit.SECONDS);
			result = true;
		} catch(Exception e) {
			e.printStackTrace();
		}
		return result;
	}

	/**
	 * 判断缓存中是否有对应的value
	 * @param key
	 * @return
	 */
	public boolean exists(final String key) {
		return redisTemplate.hasKey(key);
	}
	/**
	 * 读取缓存
	 * @param key
	 * @return
	 */
	public Object get(final String key) {
		Object result = null;
		ValueOperations<Serializable, Object> operations = redisTemplate.opsForValue();
		result = operations.get(key);
		return result;
	}
	/**
	 * 删除对应的value
	 * @param key
	 */
	public boolean remove(final String key) {
		if(exists(key)) {
			return redisTemplate.delete(key);
		}
		return false;
	}

}
